<template xmlns="http://www.w3.org/1999/html">
  <div class="content">
    <div class="title">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>系统</b></el-breadcrumb-item>
        <el-breadcrumb-item>功能列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="main-content">
      <el-col style="margin-bottom: 10px;margin-top: 5px">
        <el-form :inline="true" :model="queryContent">
          <el-form-item label="关键字">
            <el-input v-model="queryContent.condition" placeholder="功能名称/功能描述"></el-input>
          </el-form-item>
          <el-form-item label="分组名称">
            <el-select v-model="queryContent.selectedGroup" placeholder="请选择功能分组">
              <el-option v-for="group in groupOptions" :label="group.label" :key="group.value" :value="group.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="请求类型">
             <el-select v-model="queryContent.selectedMethod" placeholder="请选择请求类型">
               <el-option label="POST" value="POST"></el-option>
               <el-option label="GET" value="GET"></el-option>
               <el-option label="PUT" value="PUT"></el-option>
               <el-option label="DELETE" value="DELETE"></el-option>
               <el-option label="ALL" value="ALL"></el-option>
            </el-select>
          </el-form-item>&nbsp;&nbsp;
          <el-button type="primary" @click="queryFunc">查询</el-button>
          <el-button @click="createFunction" size="primary" >新增功能</el-button>
        </el-form>
      </el-col>
      <el-table :data="accounts" highlight-current-row @sort-change="sortIsMenu">
        <el-table-column prop="funcId" label="编号" width="100">
        </el-table-column>
        <el-table-column prop="funcName" label="功能名称" width="200">
        </el-table-column>
        <el-table-column prop="funcDesc" label="功能说明" width="200">
        </el-table-column>
        <el-table-column prop="isMenuName" label="是否菜单" width="120" sortable>
        </el-table-column>
        <el-table-column prop="method" label="请求类型" width="100">
        </el-table-column>
        <el-table-column prop="url" label="链接地址" width="200">
        </el-table-column>
        <el-table-column prop="funcGroupId" label="分组名称" width="120" v-if="false">
        </el-table-column>
        <el-table-column prop="funcGroupName" label="分组名称" width="120">
        </el-table-column>
        <el-table-column prop="isDeletedName" label="状态" width="80">
        </el-table-column>
        <el-table-column prop="options" label="操作" min-width="80" fixed="right">
            <template scope="scope">
              <el-button @click="changeData(scope.$index,scope.row)" type="text" size="small" icon="edit"></el-button>
            </template>
          </el-table-column>
      </el-table>
      <el-col>
      <el-pagination layout="sizes,prev, pager, next" @current-change="getCurrentPage"
                     :page-sizes="[10,20,30,50]" :total="total"
                     :page-size="pagesize"
                     :current-page.sync="currentPage"
                     @size-change="sizeChange"
                     style="text-align: center;margin-top: 20px">
      </el-pagination>
      </el-col>
      </div>
      <el-dialog title="新增功能" :visible.sync="dialogFormVisible">
        <el-form :model="form" style="margin-left: 120px">
          <el-form-item label="功能id" v-if="false">
            <el-input v-model="form.funcId" auto-complete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="功能名称">
            <el-input v-model="form.funcName" auto-complete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="功能说明">
            <el-input v-model="form.funcDesc" auto-complete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="链接地址">
            <el-input v-model="form.url" auto-complete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="功能代码">
            <el-input v-model="form.funcCode" auto-complete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="功能分组">
            <el-select v-model="form.funcGroupId" placeholder="请选择功能分组" style="width: 300px">
              <el-option v-for="group in groupList" :label="group.label" :key="group.value" :value="group.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否菜单">
            <el-select v-model="form.isMenu" placeholder="是否菜单" style="width: 300px">
              <el-option label="是" value='1'></el-option>
              <el-option label="否" value='0'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="菜单链接" v-show="(this.form.isMenu==1)">
            <el-input v-model="form.menuUrl" auto-complete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="请求类型">
            <el-select v-model="form.method" placeholder="请选择请求类型" style="width: 300px">
               <el-option label="POST" value="POST"></el-option>
               <el-option label="GET" value="GET"></el-option>
               <el-option label="PUT" value="PUT"></el-option>
               <el-option label="DELETE" value="DELETE"></el-option>
               <el-option label="ALL" value="ALL"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <el-select v-model="form.isDeleted" placeholder="请选择状态" style="width: 300px">
               <el-option label="启用" value="0"></el-option>
               <el-option label="关闭" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitFunc">确 定</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
  import ElCol from 'element-ui/packages/col/src/col'
  import ElForm from '../../../node_modules/element-ui/packages/form/src/form.vue'
  import axios from 'axios'
  import $ from 'jquery'

  export default {
    components: {
      ElForm,
      ElCol
    },
    data () {
      return {
        condition:'',
        dialogFormVisible:false,
        accounts:[],
        pagesize: 10,
        currentPage:1,
        total: 10,
        form:{funcId:'',funcName:'',funcDesc:'',method:'',isMenu:'',isDeleted:'',funcGroupId:'',menuUrl:'',funcCode:''},
        groupList:[],
        groupOptions:[],
        queryContent:{condition:null,selectedGroup:null,selectedMethod:null},
        isMenu:null
      }
    },
    methods:{
      submitFunc(){
        let that = this
        if(this.form.isMenu=='0'){
          this.form.menuUrl=''
        }
        $.each(this.groupList,function(index,value){
          if(value.value==that.form.funcGroupId){
            that.form.funcGroupName = value.label
          }
        })
        this.$http.post("/funcs/detail",this.form)
        .then((response)=>{
          if(response.data.success){
            that.dialogFormVisible = false
            this.$message('保存成功')
            this.getFuncList()
          }else{
            this.$message('保存失败')
          }
        })
        .catch(function(error){
          console.log(error)
        })
      },

      getGroupList(){
        let that = this
        this.$http.get("/funcs/group/list")
        .then((response)=>{
          if(response.data.success){
            $.each(response.data.data,function(index , value){
              that.groupList.push({value:value.code,label:value.name})
              that.groupOptions.push({value:value.code,label:value.name})
            })
          }
        })
        .catch(function(error){
          console.log(error)
        })
      },

      sortIsMenu(column){
        if(column.order=='ascending'){
          this.isMenu=0
        }else if(column.order=='descending'){
          this.isMenu=1
        }else{
          this.isMenu=null
        }

        this.getFuncList()
      },

      queryFunc(){
        this.getFuncList()
      },

      getFuncList(){
        this.$http.get("/funcs/list",{
          params:{
            pageSize:this.pagesize,
            pageNo:this.currentPage,
            method:this.queryContent.selectedMethod,
            funcGroupId:this.queryContent.selectedGroup,
            keyword:this.queryContent.condition,
            isMenu:this.isMenu
          }
        }).then((response)=>{
          $.each(response.data.data , function(index , value){
            if(value.isMenu=='0'){
              value.isMenuName = '否'
            }else{
              value.isMenuName = '是'
            }
            if(value.isDeleted=='0'){
              value.isDeletedName='启用'
            }else{
              value.isDeletedName='关闭'
            }
          })
          this.accounts = response.data.data
          this.total = response.data.totalCount
        }).catch(function(error){
          console.log(error)
        })
      },

      createFunction(){
        this.dialogFormVisible = true
        this.form={funcId:'',funcName:'',funcDesc:'',method:'',isMenu:'',isDeleted:'',funcGroupId:'',menuUrl:''}
      },

      changeData(index , row){
        this.dialogFormVisible = true
        //this.form.isMenu = '1'
        this.form = row
        if(row.isMenu=='0'){
          this.form.isMenu = '0'
        }else{
          this.form.isMenu='1'
        }
        if(row.isDeleted=='0'){
          this.form.isDeleted='0'
        }else{
          this.form.isDeleted='1'
        }
        var groupId = row.funcGroupId
        this.form.funcGroupId=JSON.stringify(groupId)
      },

      getCurrentPage(currentPage){
        this.currentPage = currentPage
        this.getFuncList()
      },

      sizeChange(val){
        this.pagesize = val
        this.getFuncList()
      }
     
    },

    mounted() {
      this.getFuncList()
      this.getGroupList()
    },
    beforeMount(){
      this.getFuncList()
    }
  }
</script>

<style lang="scss" scoped>
.content {
  padding: 10px;
  background: #F7F7F7;
  .title {
    color: #99A9BF;
    font-size: 14px;
    line-height: 18px;
    margin: 8px 0px 10px 0px;
  }
  .main-content {
    background: #fff;
    position: fixed;
    top: 96px;
    bottom: 10px;
    left: 216px;
    right: 16px;
    overflow: auto;
    padding: 20px;
  }
}
</style>